package com.example.musicplayer.adapter

import android.graphics.BitmapFactory
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.musicplayer.R
import com.example.musicplayer.model.Song

class HistoryAdapter(private val history: List<Song>) : RecyclerView.Adapter<HistoryAdapter.HistoryViewHolder>() {

    class HistoryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageViewSong: ImageView = itemView.findViewById(R.id.imageViewSong)
        val textViewTitle: TextView = itemView.findViewById(R.id.textViewTitle)
        val textViewArtist: TextView = itemView.findViewById(R.id.textViewArtist)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_song, parent, false)
        return HistoryViewHolder(view)
    }

    override fun onBindViewHolder(holder: HistoryViewHolder, position: Int) {
        val song = history[position]
        holder.textViewTitle.text = song.title
        holder.textViewArtist.text = song.artist
        holder.imageViewSong.setImageBitmap(song.image?.let { BitmapFactory.decodeByteArray(it, 0, it.size) })
    }

    override fun getItemCount(): Int {
        return history.size
    }
}